package util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

import reporting.FileWork;

public class TestConfiguration {	
	
	public static TestConfiguration getInstance() {
	      return INSTANCE;
	}
	
	public void loadLocalProperties(String fullPath) {
		fullPath = fullPath.concat(".properties");
		
		if (fs.existsFileOrDir(fullPath)) {
			try {
				properties.load(new FileInputStream(fullPath));
			} catch(IOException e) {
				System.err.print(e.getMessage());
			}
		}
	}
	
	public String getEnv(String name) {
		return properties.getProperty(name.toLowerCase().trim(), "[NO FOUND]");
	}
    
	public void setEnv(String key, String value) {
		Util u = new Util();
		if(!properties.containsKey(key)) {
			properties.put(key, value);
		} else {
			System.err.println("Error: The '" + key 
					+ "' dynamic variable already exists! \n Debug info: ("
					+" key = '" + key +  "', value = '" + u.empVl(value) + "')");
		}
	}
	
	public void deleteLocalAndDynamicEnv() {
		properties = new Properties(propertiesDefault);
	}
	
	private FileWork fs = FileWork.getInstance();
	private Properties propertiesDefault = new Properties();
	private Properties properties;
	private TestConfiguration() {
		try {
			propertiesDefault.load(new FileInputStream("global.properties"));
		} catch(IOException e) {
			System.err.print(e.getMessage());
		}		
		properties = new Properties(propertiesDefault);			
	}
	
	private static final TestConfiguration INSTANCE = new TestConfiguration();
}
